// import * as Matter from 'matter-js';

import { generate, getAllPath } from "./parse";

export default class MainScene extends Phaser.Scene {
  block!: Phaser.Physics.Matter.Image;
  constructor() {
    super("Main");
  }
  // 1、Draw Stiff Line
  // 2、Rotate Body With Cursors
  // 3、Compound Body
  create() {
    const resize = () => {
      this.scale.resize(window.innerWidth, window.innerHeight);
    };
    window.addEventListener("resize", resize);
    resize();

    this.matter.world.setGravity(0, 1); // 设置重力方向
    const Bodies = Phaser.Physics.Matter.Matter.Bodies;
    // 方案1：简单镐头（水平十字结构）
    const handle = Bodies.rectangle(400, 300, 120, 20); // 手柄
    const pickHead = Bodies.rectangle(400, 300, 40, 40, {
      render: { fillStyle: "#666" },
      chamfer: { radius: 15 }, // 倒角使形状更接近镐头
    });

    const longHandle = Bodies.rectangle(400, 350, 20, 200);

    const compoundBody = Phaser.Physics.Matter.Matter.Body.create({
      parts: [handle, pickHead, longHandle],
      restitution: 0.5,
      gravityScale: 1, // 允许受重力
      mass: 8, // 合理质量值
    });

    this.matter.add.gameObject(this.add.rectangle(0, 0, 1, 1), compoundBody);
    const ground = this.matter.add.rectangle(400, 800, 800, 50, {
      isStatic: true,
    });

    generate(800, 600);
    const allPath = getAllPath();
    console.log(allPath, "allPath");
    // const path = allPath[0].path;
    const path = `M0 60C10.67 61.02 28.03 67.23 23.05 55.23C18.08 43.23 39.41 43.23 33.72 55.23C28.03 67.23 42.67 58.46 53.33 60L0 0L53.33 0L53.33 0C54.08 12 46.91 29.66 57.57 24.71C68.24 19.76 68.24 43.76 57.57 36.71C46.91 29.66 53.9 48 53.33 60L0 0L0 60L100,100`;
    console.log(path);

    console.log("Path是否闭合:", /Z\s*$/i.test(path)); // 应输出 true
    const vertices = Phaser.Physics.Matter.Matter.Vertices.fromPath(path, true);
    console.log("生成顶点数:", vertices.length); // 正常应输出 >3 的数字

    this.matter.add.fromVertices(200, 300, vertices, {
      isStatic: false,
      restitution: 0.5,
    });
  }
  update(time: number, delta: number): void {}
}
